//
//  MediaViewController.m
//  YongChuanTradingArea
//
//  Created by Vivian on 2017/9/13.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "MediaViewController.h"
#import "MediaCell.h"
#import "MinePromptViewController.h"

static NSString * const MediaCellID = @"MediaCell";

@interface MediaViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *postBtn;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@end

@implementation MediaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"自媒体";
    self.postBtn.layer.cornerRadius = 15;
    self.postBtn.layer.masksToBounds = YES;
    self.postBtn.layer.borderWidth = 1;
    self.postBtn.layer.borderColor = kOrangeColor.CGColor;
    self.view.backgroundColor = kBgColor;
    self.tableView.estimatedRowHeight = 80;
    self.tableView.frame = CGRectMake(0, CGRectGetMaxY(self.countLabel.frame), lbScreenWidth, lbScreenHeight - CGRectGetMaxY(self.countLabel.frame) - 64);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:MediaCellID bundle:nil] forCellReuseIdentifier:MediaCellID];
    [self setupPageHeader:YES footer:YES];
    [self headerRefresh];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MediaCell * cell = [tableView dequeueReusableCellWithIdentifier:MediaCellID forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - btn clicked 
- (IBAction)postBtnAction:(UIButton *)sender {
    MinePromptViewController * vc = [[MinePromptViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - request data
- (void)requestPageData:(void (^)(YCNetworkingStatusCode, NSArray *))statusBlock{
    __weak typeof(self) weakSelf = self;
    [LBNetworkingInstance requestGET:memberList parameters:@{@"memberId":[LBUserManage defaultManager].userModel.ID?:@"", @"pageIndex":@(self.pageIndex),@"pageSize":@(self.pageSize)} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        if (code == YCNetworkingStatusCodeSuccess){
            NSArray * array = [NSArray yy_modelArrayWithClass:[MineMediaModel class] json:responseObject[@"Data"]];
            statusBlock(code, array);
            weakSelf.countLabel.text = [NSString stringWithFormat:@"推广人数 %@人",responseObject[@"data_total"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        }else{
            statusBlock(code, nil);
        }
    } failure:^(NSError *err) {
        statusBlock(YCNetworkingStatusCodeHTTPError, nil);
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
